{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44f48d8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "import os\n",
    "\n",
    "os.environ['HUGGINGFACEHUB_API_TOKEN'] = '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'\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad5ba61e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from langchain import HuggingFaceHub, LLMChain\n",
    "\n",
    "from langchain.chains import ConversationChain\n",
    "\n",
    "from langchain.chains.conversation.memory import ConversationBufferMemory\n",
    "\n",
    "# initialize Hub LLM\n",
    "hub_llm = HuggingFaceHub(\n",
    "        repo_id='google/flan-t5-large',\n",
    "    # model_kwargs={'temperature':1e-10}\n",
    ")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "188d9837",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import LLMMathChain\n",
    "from langchain.agents import Tool\n",
    "\n",
    "llm_math = LLMMathChain(llm = hub_llm)\n",
    "# 初始化 math 工具\n",
    "math_tool = Tool(\n",
    "    name='Calculator',\n",
    "    func=llm_math.run,\n",
    "    description='Useful for when you need to answer questions about math.'\n",
    ")\n",
    "\n",
    "tools = [math_tool]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71a8e55e",
   "metadata": {},
   "outputs": [],
   "source": [
    "tools[0].name, tools[0].description"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f068f001",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import initialize_agent\n",
    "\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "\n",
    "zero_shot_agent = initialize_agent(\n",
    "    agent='zero-shot-react-description',\n",
    "    tools=tools,\n",
    "    llm=hub_llm,\n",
    "    verbose=True,\n",
    "    handle_parsing_errors=True,\n",
    "    max_iterations = 3\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3471b4c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "zero_shot_agent(\"1*1=?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06abcfec",
   "metadata": {},
   "outputs": [],
   "source": [
    "zero_shot_agent(\"if Mary has four apples and Giorgio brings two and a half apple \"\n",
    "                \"boxes (apple box contains eight apples), how many apples do we \"\n",
    "                \"have?\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
